package com.shxzz.shortlink.admin.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shxzz.shortlink.admin.common.convention.result.Result;
import com.shxzz.shortlink.admin.remote.dto.req.*;
import com.shxzz.shortlink.admin.remote.dto.resp.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author shxzz
 * @create 2023-12-19-10:52
 * @description 短链接服务 HTTP 调用接口
 */
public interface LinkRemoteService {

    String REMOTE_URL = "http://localhost:8002";

    /**
     * 生成短链接
     *
     * @param requireParam 短链接生成请求参数
     * @return 短链接生成信息
     */
    default Result<LinkCreateRespDTO> createLink(LinkCreateReqDTO requireParam) {
        String remoteResponse = HttpUtil.post(REMOTE_URL + "/api/short-link/v1/create", JSON.toJSONString(requireParam));
        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 分页查询短链接
     *
     * @param requireParam 分页查询请求参数
     * @return 分页查询结果
     */
    default Result<IPage<LinkPageRespDTO>> selectPageLink(LinkPageReqDTO requireParam) {
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid", requireParam.getGid());
        requestMap.put("current", requireParam.getCurrent());
        requestMap.put("size", requireParam.getSize());

        String remoteResponse = HttpUtil.get(REMOTE_URL + "/api/short-link/v1/page", requestMap);
        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 短链接信息修改
     * @param requireParam 短链接信息修改请求参数
     */
    default Result<Void> updateLink(LinkUpdateReqDTO requireParam){

        String remoteResponse = HttpUtil.post(REMOTE_URL + "/api/short-link/v1/update", JSON.toJSONString(requireParam));
        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 短链接组内数量查询
     *
     * @param gids 查询分组 id 列表
     * @return 短链接组内数量统计结果
     */
    default Result<List<LinkGroupCountRespDTO>> selectLinkGroupCount(List<String> gids) {
        String remoteResponse = HttpUtil.post(REMOTE_URL + "/api/short-link/v1/count", JSON.toJSONString(gids));
        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 移入回收站
     * @param requireParam 移入回收站短链接参数
     */
    default Result<Void> moveToRecycleBin(RecycleBinSaveReqDTO requireParam) {
        String remoteResponse = HttpUtil.post(REMOTE_URL + "/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requireParam));
        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 回收站列表
     * @param requireParam 回收站分页查询参数
     * @return 分页查询结果
     */
    default Result<IPage<RecycleBinLinkPageRespDTO>> selectPageRecycleBin(RecycleBinPageReqDTO requireParam) {
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList", requireParam.getGidList());
        requestMap.put("current", requireParam.getCurrent());
        requestMap.put("size", requireParam.getSize());

        String remoteResponse = HttpUtil.get(REMOTE_URL + "/api/short-link/v1/recycle-bin/page", requestMap);
        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 回收站还原
     * @param requireParam 回收站还原短链接请求参数
     */
    default Result<Void> restoreRecycleBinLink(RecycleBinRestoreReqDTO requireParam) {
        String remoteResponse = HttpUtil.post(REMOTE_URL + "/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requireParam));
        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 回收站彻底删除
     * @param requireParam 从回收站删除链接请求参数
     */
    default Result<Void> removeRecycleBinLink(RecycleBinRemoveReqDTO requireParam) {
        String remoteResponse = HttpUtil.post(REMOTE_URL + "/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requireParam));
        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 访问单个短链接指定时间内监控数据
     */
    default Result<LinkStatusRespDTO> shortLinkStats(LinkStatusReqDTO requestParam) {
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("fullShortUrl", requestParam.getFullShortUrl());
        requestMap.put("gid", requestParam.getGid());
        requestMap.put("startDate", requestParam.getStartDate());
        requestMap.put("endDate", requestParam.getEndDate());

        String remoteResponse = HttpUtil.get(REMOTE_URL + "/api/short-link/v1/stats", requestMap);

        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 访问单个短链接指定时间内访问记录监控数据
     */
    default Result<IPage<LinkAccessRecordRespDTO>> shortLinkStatsAccessRecord(LinkAccessRecordReqDTO requestParam) {
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("fullShortUrl", requestParam.getFullShortUrl());
        requestMap.put("gid", requestParam.getGid());
        requestMap.put("startDate", requestParam.getStartDate());
        requestMap.put("endDate", requestParam.getEndDate());

        String remoteResponse = HttpUtil.get(REMOTE_URL + "/api/short-link/v1/stats/access-record", requestMap);

        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 访问分组短链接指定时间内监控数据
     */
    default Result<GroupLinkStatusRespDTO> groupShortLinkStats(GroupLinkStatusReqDTO requestParam) {
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid", requestParam.getGid());
        requestMap.put("startDate", requestParam.getStartDate());
        requestMap.put("endDate", requestParam.getEndDate());

        String remoteResponse = HttpUtil.get(REMOTE_URL + "/api/short-link/v1/stats/group", requestMap);

        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 访问分组短链接指定时间内访问记录监控数据
     */
    default Result<IPage<GroupLinkAccessRecordRespDTO>> groupShortLinkStatsAccessRecord(GroupLinkAccessRecordReqDTO requestParam) {
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid", requestParam.getGid());
        requestMap.put("startDate", requestParam.getStartDate());
        requestMap.put("endDate", requestParam.getEndDate());

        String remoteResponse = HttpUtil.get(REMOTE_URL + "/api/short-link/v1/stats/access-record/group", requestMap);

        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }

    /**
     * 获取目标网址标题
     */
    default Result<String> getTargetUrlTitle(String targetUrl) {
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("url", targetUrl);

        String remoteResponse = HttpUtil.get(REMOTE_URL + "/api/short-link/v1/title", requestMap);

        return JSON.parseObject(remoteResponse, new TypeReference<>() {
        });
    }
}
